﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing;

using H2.NET.Controls;
using H2.NET.Business;
using H2.NET.Data.Common;

using System.IO;
using H2.NET.Data.Exceller;



namespace H2.NET.Web.Pages
{
    public partial class PRMM : TaskPage
    {
        #region 属性 Public

        private bool IsLoaded
        {
            get
            {
                return ViewState["IsLoaded"] == null ? false : (bool)ViewState["IsLoaded"];
            }
            set { ViewState["IsLoaded"] = value; }
        }

        public string Key
        {
            get
            {
                return ViewState["Key"] == null ? "" : (string)ViewState["Key"];

            }
            set { ViewState["Key"] = value; }
        }

        private string[] Sales_Office
        {
            get
            {
                if (StoreAPI["Sales_Office~" + Key] == null)
                {

                    IList<m_ListPrice_SalesOffice> dlts = (m_ListPrice_SalesOffice.status == "1").List<m_ListPrice_SalesOffice>();
                    string[] sales_office = new string[dlts.Count + 1];

                    sales_office[0] = "";
                    for (int i = 0; i < dlts.Count; i++)
                    {
                        sales_office[i + 1] = dlts[i].Sales_Office;
                    }

                    StoreAPI["Sales_Office~" + Key] = sales_office;
                }

                return (string[])StoreAPI["Sales_Office~" + Key];
            }
        }

        private string[] LPM_Region
        {
            get
            {
                if (StoreAPI["LPM_Region~" + Key] == null)
                {

                    IList<m_ListPrice_Region> dlts = (m_ListPrice_Region.status == "1").List<m_ListPrice_Region>();
                    string[] lpm_region = new string[dlts.Count + 1];

                    lpm_region[0] = "";
                    for (int i = 0; i < dlts.Count; i++)
                    {
                        lpm_region[i + 1] = dlts[i].Region;
                    }

                    StoreAPI["LPM_Region~" + Key] = lpm_region;
                }

                return (string[])StoreAPI["LPM_Region~" + Key];
            }
        }

        private string[] City_Code
        {
            get
            {
                if (StoreAPI["City_Code~" + Key] == null)
                {

                    IList<m_ListPrice_City> dlts = (m_ListPrice_City.status == "1").List<m_ListPrice_City>();
                    string[] city_code = new string[dlts.Count + 1];

                    city_code[0] = "";
                    for (int i = 0; i < dlts.Count; i++)
                    {
                        city_code[i + 1] = dlts[i].City;
                    }

                    StoreAPI["City_Code~" + Key] = city_code;
                }

                return (string[])StoreAPI["City_Code~" + Key];
            }
        }
        #endregion


        #region 事件 Page_Load(页面加载)
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                INFO.Text = string.Format("<br/>表单正在被同事“{0}”抢占处理，拒绝提交！", RobUser);
                INFO.Visible = IsOverFlow;

                //InitDropDownList();

                #region 发起步骤
                if (TaskAPI.IsInitiate)
                {
                    // VD.Checked = true;

                }
                #endregion

                #region 审批环节
                else
                {
                    #region 加载数据并显示
                    prm_promotion listp = (prm_promotion.process == TaskAPI.Process & prm_promotion.incident == TaskAPI.Incident).ListFirst<prm_promotion>();
                    if (listp != null)
                    {
                        ApplicationNo.Text = listp.ApplicationNo;
                        //VD.Checked                          = vendor.VD_Checked == "1";
                        //SA.Checked = vendor.SA_Checked == "1";
                        VD_Category.SelectedValue = listp.VD_Category;
                        DrpPriority.SelectedValue = listp.DrpPriority;
                        //Condition_Type.Text = listp.Condition_Type;
                        //Sorg_DC.Checked = listp.Sorg_DC == "1";
                        //Sales_Organization.Text = listp.Sales_Organization;
                        //Distribution_Channel.Text = listp.Distribution_Channel;
                        //Division.SelectedValue = listp.Division;

                        DMO_DrpPriority.SelectedValue = listp.DMO_DrpPriority;
                        DMO_Arrpvoe_Creation_Number.Text = listp.DMO_Arrpvoe_Creation_Number;
                        DMO_Arrpvoe_Creation_Time.Text = listp.DMO_Arrpvoe_Creation_Time;
                        DMO_Maintain_Creation_Number.Text = listp.DMO_Maintain_Creation_Number;
                        DMO_Maintain_Creation_Time.Text = listp.DMO_Maintain_Creation_Time;
                        DMO_Check_Creation_Number.Text = listp.DMO_Check_Creation_Number;
                        DMO_Check_Creation_Time.Text = listp.DMO_Check_Creation_Time;
                        DMO_Arrpvoe_Revision_Number.Text = listp.DMO_Arrpvoe_Revision_Number;
                        DMO_Arrpvoe_Revision_Time.Text = listp.DMO_Arrpvoe_Revision_Time;
                        DMO_Maintain_Revision_Number.Text = listp.DMO_Maintain_Revision_Number;
                        DMO_Maintain_Revision_Time.Text = listp.DMO_Maintain_Revision_Time;
                        DMO_Check_Revision_Number.Text = listp.DMO_Check_Revision_Number;
                        DMO_Check_Revision_Time.Text = listp.DMO_Check_Revision_Time;

                    }
                    #endregion

                    #region 审批项初始化
                    switch (TaskAPI.Step)
                    {
                        case "DMO Approve":
                        case "DMO Maintain":
                        case "DMO Check":
                        case "Trade Marketing Check":
                            Assessment1.Option.Add(new t_AssessmentOption("Finish", "1", true));
                            Assessment1.Option.Add(new t_AssessmentOption("Return", "2", false));
                            break;

                    }
                    #endregion
                }
                #endregion

                #region 设置控件禁启用
                bool flg = (TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit) ? true : false;

                SetEnable(flg, VD_Category,DrpPriority);
                //SetEnable(TaskAPI.Step == "DMO Maintain", SAP_Code);
                //SetVisible(flg, List_Price_Add, List_Price_Delete);
                SetEnable(TaskAPI.Step == "DMO Approve" | TaskAPI.Step == "DMO Maintain" | TaskAPI.Step == "DMO Check", DMO_DrpPriority, DMO_Arrpvoe_Creation_Number, DMO_Arrpvoe_Creation_Time, DMO_Maintain_Creation_Number, DMO_Maintain_Creation_Time, DMO_Check_Creation_Number, DMO_Check_Creation_Time, DMO_Arrpvoe_Revision_Number, DMO_Arrpvoe_Revision_Time, DMO_Maintain_Revision_Number, DMO_Maintain_Revision_Time, DMO_Check_Revision_Number, DMO_Check_Revision_Time);
                SetVisible(TaskAPI.Step == "DMO Approve" | TaskAPI.Step == "DMO Maintain" | TaskAPI.Step == "DMO Check", P_DMO);
                Assessment1.Enable = TaskAPI.IsAllowSubmit;
                SetVisible(TaskAPI.IsAllowSubmit, Send, Split);

                //if (TaskAPI.Step == "DMO Approve")
                //{
                //    SetForeColor(Color.Blue, DMO_DrpPriority_L);
                //}

                if (TaskAPI.Step == "DMO Approve" & VD_Category.SelectedValue == "1")
                {
                    SetForeColor(Color.Blue, DMO_Arrpvoe_Creation_L);
                }
                if (TaskAPI.Step == "DMO Maintain" & VD_Category.SelectedValue == "1")
                {
                    SetForeColor(Color.Blue, DMO_Maintain_Creation_L);
                }
                if (TaskAPI.Step == "DMO Check" & VD_Category.SelectedValue == "1")
                {
                    SetForeColor(Color.Blue, DMO_Check_Creation_L);
                }
                if (TaskAPI.Step == "DMO Approve" & VD_Category.SelectedValue != "1")
                {
                    SetForeColor(Color.Blue, DMO_Arrpvoe_Revision_L);
                }
                if (TaskAPI.Step == "DMO Maintain" & VD_Category.SelectedValue != "1")
                {
                    SetForeColor(Color.Blue, DMO_Maintain_Revision_L);
                }
                if (TaskAPI.Step == "DMO Check" & VD_Category.SelectedValue != "1")
                {
                    SetForeColor(Color.Blue, DMO_Check_Revision_L);
                }

                Attachment1.AllowDelete = TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit;
                Attachment1.AllowUpload = TaskAPI.IsBeginStep && TaskAPI.IsAllowSubmit;
                //InitTabs();
                #endregion

                #region 加深颜色
                string item = null;
                string value = null;
                foreach (Control cr in this.Controls)
                {
                    if (cr is DropDownList)
                    {
                        DropDownList tb = (DropDownList)cr;
                        if (!tb.Enabled)
                        {
                            tb.Enabled = true;
                            item = tb.SelectedItem.Text;
                            value = tb.SelectedValue.ToString();
                            tb.Items.Clear();
                            tb.Items.Add(new ListItem(item, value));
                        }
                        tb.ForeColor = System.Drawing.Color.DimGray;
                    }
                }
                if (!flg || !TaskAPI.IsAllowSubmit)
                {

                    foreach (Control cr in form.Controls)
                    {
                        if (cr is DropDownList)
                        {
                            DropDownList tb = (DropDownList)cr;
                            if (!tb.Enabled)
                            {
                                tb.Enabled = true;
                                item = tb.SelectedItem.Text;
                                value = tb.SelectedValue.ToString();
                                tb.Items.Clear();
                                tb.Items.Add(new ListItem(item, value));
                            }
                            tb.ForeColor = System.Drawing.Color.DimGray;
                        }
                    }
                    foreach (Control cr in form.Controls)
                    {
                        if (cr is TextBox)
                        {
                            TextBox tb = (TextBox)cr;
                            if (!tb.Enabled)
                            {
                                tb.Enabled = true;
                                tb.ReadOnly = true;
                                tb.ForeColor = System.Drawing.Color.DimGray;
                            }
                        }
                    }

                }

                #endregion

            }
        }


        protected override void OnLoad(EventArgs e)
        {
            InitControls();
            base.OnLoad(e);
        }

        public void InitControls()
        {
        //    bool flg = TaskAPI.IsAllowSubmit && TaskAPI.IsBeginStep;
        //    gv_List_Price.DynamicColumn.Add(new BoundColumn2("00", "No", BoundColumnType2.Label, 0, Align.Left, false, "No", InputType.None, null));
        //    gv_List_Price.DynamicColumn.Add(new BoundColumn2("01", "Subline<br/>(子产品线)", BoundColumnType2.TextBox, 60, Align.Left, flg, "Subline", InputType.None, null));
        //    gv_List_Price.DynamicColumn.Add(new BoundColumn2("02", "Price Grp", BoundColumnType2.DropDownList, 55, Align.Left, flg, "Price_Grp", InputType.None, new string[9] { "", "KA", "KB", "KC", "KD", "KE", "KF", "KG", "KH" }));
        //    gv_List_Price.DynamicColumn.Add(new BoundColumn2("03", "County Code<br/>(县级代码)", BoundColumnType2.DropDownList, 80, Align.Left, flg, "County_Code", InputType.None, new string[5] { "", "K01", "K02", "K03", "K04" }));
        //    gv_List_Price.DynamicColumn.Add(new BoundColumn2("04", "Sales Off.<br/>(销售办公室)", BoundColumnType2.DropDownList, 80, Align.Left, flg, "Sales_Office", InputType.None, Sales_Office));
        //    gv_List_Price.DynamicColumn.Add(new BoundColumn2("05", "Region<br/>(省份代码)", BoundColumnType2.DropDownList, 60, Align.Left, flg, "LPM_Region", InputType.None, LPM_Region));
        //    gv_List_Price.DynamicColumn.Add(new BoundColumn2("06", "City Code<br/>(城市代码)", BoundColumnType2.DropDownList, 60, Align.Left, flg, "City_Code", InputType.None, City_Code));
        //    gv_List_Price.DynamicColumn.Add(new BoundColumn2("07", "PH4<br/>(hierarchy)", BoundColumnType2.TextBox, 60, Align.Left, flg, "PH4", InputType.None, null));
        //    gv_List_Price.DynamicColumn.Add(new BoundColumn2("08", "Tin size<br/>(包装)", BoundColumnType2.TextBox, 60, Align.Left, flg, "Tin_Size", InputType.None, null));
        //    gv_List_Price.DynamicColumn.Add(new BoundColumn2("09", "Status", BoundColumnType2.TextBox, 60, Align.Left, flg, "LP_Status", InputType.None, null));
        //    gv_List_Price.DynamicColumn.Add(new BoundColumn2("10", "Description<br/>(产品名)", BoundColumnType2.TextBox, 60, Align.Left, flg, "Description", InputType.None, null));
        //    gv_List_Price.DynamicColumn.Add(new BoundColumn2("11", "Proc.Stat", BoundColumnType2.TextBox, 60, Align.Left, flg, "Proc_Stat", InputType.None, null));
        //    gv_List_Price.DynamicColumn.Add(new BoundColumn2("12", "Scale Quantity", BoundColumnType2.TextBox, 60, Align.Left, flg, "Scale_Quantity", InputType.None, null));
        //    gv_List_Price.DynamicColumn.Add(new BoundColumn2("13", "Amount<br/>(金额)", BoundColumnType2.TextBox, 60, Align.Left, flg, "Amount", InputType.None, null));
        //    gv_List_Price.DynamicColumn.Add(new BoundColumn2("14", "Unit", BoundColumnType2.TextBox, 60, Align.Left, flg, "Unit", InputType.None, null));
        //    gv_List_Price.DynamicColumn.Add(new BoundColumn2("15", "Per", BoundColumnType2.TextBox, 60, Align.Left, flg, "Per", InputType.None, null));
        //    gv_List_Price.DynamicColumn.Add(new BoundColumn2("16", "UoM<br/>(单位)", BoundColumnType2.TextBox, 60, Align.Left, flg, "Uom", InputType.None, null));
        //    gv_List_Price.DynamicColumn.Add(new BoundColumn2("17", "Calculat.type", BoundColumnType2.TextBox, 60, Align.Left, flg, "Calculat_Type", InputType.None, null));
        //    gv_List_Price.DynamicColumn.Add(new BoundColumn2("18", "ScaleBasis", BoundColumnType2.TextBox, 60, Align.Left, flg, "ScaleBasis", InputType.None, null));
        //    gv_List_Price.DynamicColumn.Add(new BoundColumn2("19", "Valid From<br/>(起始日期)", BoundColumnType2.Calender, 60, Align.Left, flg, "Valid_From", InputType.None, null));
        //    gv_List_Price.DynamicColumn.Add(new BoundColumn2("20", "Valid To<br/>(结束日期)", BoundColumnType2.Calender, 60, Align.Left, flg, "Valid_To", InputType.None, null));
        //    gv_List_Price.Width = 1500;



        //    if (!Page.IsPostBack)
        //    {
        //        gv_List_Price.Key = Key;


        //        //BOM_P1.Visible = true;

        //        #region 发起步骤
        //        if (TaskAPI.IsInitiate)
        //        {
        //            List<prm_promotion1> lpm_s = new List<prm_promotion1>();
        //            for (int i = 0; i < 5; i++)
        //            {
        //                lpm_s.Add(new prm_promotion1());
        //                lpm_s[i].No = i + 1;
        //            }
        //            gv_List_Price.DataSource = lpm_s;


        //        }
        //        #endregion

        //        #region 审批环节
        //        else
        //        {
        //            IList<prm_promotion1> lpm_s = (prm_promotion1.process == TaskAPI.Process & prm_promotion1.incident == TaskAPI.Incident).List<prm_promotion1>();
        //            if (lpm_s.Count == 0)
        //            {
        //                for (int i = 0; i < 5; i++)
        //                {
        //                    lpm_s.Add(new prm_promotion1());
        //                    lpm_s[i].No = i + 1;
        //                }
        //            }
        //            gv_List_Price.DataSource = lpm_s;


        //        }
        //        #endregion
        //    }
        //    //SetVisible(flg, SEFI_1_Add, SEFI_2_Add);
        }
        #endregion






        #region 事件 Send
        protected void Send_Click(object sender, EventArgs e)
        {
            try
            {
                #region 数据验证
                if ((TaskAPI.IsBeginStep || TaskAPI.Step == "DMO Maintain" || TaskAPI.Step == "DMO Check" || TaskAPI.Step == "DMO Approve") && TaskAPI.IsAllowSubmit) // 如果是退回的情况，不存在验证
                {
                    List<string> vald_s = new List<string>();
                    vald_s.AddRange(DataValidation());
                    if (vald_s.Count > 0)
                    {
                        Anthem.Manager.AddScriptForClientSideEval(string.Format("ShowMessage(\"{0}\", {1});", CombineMessage(vald_s), vald_s.Count));
                        return;
                    }
                }
                #endregion

                #region 引擎传值
                if (TaskAPI.IsBeginStep)
                {
                    if (TaskAPI.IsInitiate)
                    {
                        ApplicationNo.Text = Util.GenerateSequence(((object[])TaskAPI["PrefixNo"])[0].ToString() + "-" + System.DateTime.Now.ToString("yyMM"), "", 4);
                        TaskAPI.UserName = UserAPI.UserName;
                        TaskAPI["ApplicationNo"] = ApplicationNo.Text;
                        TaskAPI["ApplicatorName"] = UserAPI.UserFullName;
                        TaskAPI["Applicator"] = UserAPI.UserName;
                        TaskAPI["Time"] = System.DateTime.Now.ToString("yyyy.MM.dd HH:mm:ss");
                        TaskAPI["ApplicatorEmail"] = UserAPI.Mail;
                        TaskAPI["Department"] = UserAPI.Department;
                        TaskAPI["ServerName"] = Util.AppSettings("ServerName");
                    }

                    //TaskAPI.Summary = (VD_Category.SelectedItem.Text.Split(',').Length >= 2 ? VD_Category.SelectedItem.Text.Split(',')[1].Trim() : VD_Category.Text)
                    //                + " / " + ApplicationNo.Text + (VD.Checked ? " / Vendor " : "") + (SA.Checked ? " / Employee " : "");

                    TaskAPI.Summary = (VD_Category.SelectedItem.Text.Split(',').Length >= 2 ? VD_Category.SelectedItem.Text.Split(',')[1].Trim() : VD_Category.Text) + " / " + ApplicationNo.Text;



                    TaskAPI["Actions"] = VD_Category.SelectedValue;
                    TaskAPI["Remark_by_Initiator"] = Assessment1.Assessment.Remark;
                    TaskAPI["DrpPriority"] = DrpPriority.SelectedItem.Text;

                    #region 从数据库取得流程步骤处理人
                    SortedList<string, string> rec_a = new SortedList<string, string>();

                    IList<mdc_Recipient> rec_s = (mdc_Recipient.process == TaskAPI.Process & (mdc_Recipient.distribute == VD_Category.SelectedValue | mdc_Recipient.distribute == "*") & mdc_Recipient.status == 1).List<mdc_Recipient>();
                    foreach (mdc_Recipient rec in rec_s)
                    {
                        if (!rec_a.ContainsKey(rec.Step))
                        {
                            rec_a.Add(rec.Step, rec.Recipient);
                            continue;
                        }

                        if (rec_a.Values[rec_a.IndexOfKey(rec.Step)].Contains(rec.Recipient))
                        {
                            continue;
                        }

                        rec_a[rec.Step] = rec_a.Values[rec_a.IndexOfKey(rec.Step)] + "," + rec.Recipient;
                    }

                    //步骤处理人赋值
                    for (int i = 0; i < rec_a.Keys.Count; i++)
                    {
                        TaskAPI["R_" + rec_a.Keys[i].Replace(" ", "_")] = rec_a.Values[i].Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                    }

                    m_OrgChart oc = (m_OrgChart.empID.In(m_OrgChart.supervisorEmpID, m_OrgChart.winID == UserAPI.UserName)).ListFirst<m_OrgChart>();
                    if (oc != null)
                    {
                        TaskAPI["R_Marketing_Manager"] = oc.WinID;
                    }



                    #endregion
                }
                else
                {
                    TaskAPI["ApproveResult"] = Assessment1.Assessment.Opinion;
                    TaskAPI["TaskID"] = TaskAPI.TaskID;
                }



               

                #endregion

                #region 提交步骤
                bool flg = false;
                Storage.TransactionExecute(o =>
                {
                    #region “Fill in the form”步骤
                    if (TaskAPI.IsBeginStep)
                    {
                        prm_promotion listp = new prm_promotion();
                        if ((prm_promotion.process == TaskAPI.Process & prm_promotion.incident == TaskAPI.Incident & prm_promotion.userName == UserAPI.UserName).Count<prm_promotion>() > 0)
                        {
                            listp = (prm_promotion.process == TaskAPI.Process & prm_promotion.incident == TaskAPI.Incident & prm_promotion.userName == UserAPI.UserName).ListFirst<prm_promotion>();
                        }
                        else
                        {
                            listp.Process = TaskAPI.Process;
                            listp.Incident = TaskAPI.Incident;
                            listp.UserName = UserAPI.UserName;
                            listp.UserFullName = UserAPI.UserFullName;
                            listp.ApplicationNo = ApplicationNo.Text;
                            listp.Time = System.DateTime.Now;
                            listp.Status = 0;
                            listp.Department = UserAPI.Department;
                        }

                        listp.VD_Category = VD_Category.SelectedValue;
                        listp.DrpPriority = DrpPriority.SelectedValue;
                        //prm_promotion.Actions = VD_Category.SelectedItem.Text;
                        //listp.Sorg_DC = Sorg_DC.Checked ? "1" : "0";
                        //listp.Condition_Type = Condition_Type.Text;
                        //listp.Sales_Organization = Sales_Organization.Text;
                        //listp.Division = Division.SelectedValue;
                        //listp.Distribution_Channel = Distribution_Channel.Text;


                        


                        (t_Attachment.process == TaskAPI.Process & t_Attachment.incident == 0 & t_Attachment.username == UserAPI.UserName).Edit<t_Attachment>(p =>
                        {
                            p.Incident = TaskAPI.Incident;
                        });

                        Storage.Save(Storage.Context1, listp);
                    }
                    #endregion

                    //Save();

                    //#region “DMO Maintain”步骤
                    //if (TaskAPI.Step == "DMO Maintain")
                    //{
                    //    (prm_promotion.process == TaskAPI.Process & prm_promotion.incident == TaskAPI.Incident).Edit<prm_promotion>(p =>
                    //    {
                    //        p.SAP_Code = SAP_Code.Text;
                    //    }    
                    //    );
                    //}
                    //#endregion

                    #region Priority功能
                    if (TaskAPI.IsBeginStep && !string.IsNullOrEmpty(DrpPriority.SelectedValue))
                    {
                        mdc_Priority pro;
                        if ((mdc_Priority.process == TaskAPI.Process & mdc_Priority.incident == TaskAPI.Incident).Count<mdc_Priority>() > 0)
                        {
                            pro = new mdc_Priority();
                            pro = (mdc_Priority.process == TaskAPI.Process & mdc_Priority.incident == TaskAPI.Incident).ListFirst<mdc_Priority>();
                            pro.Business_Priority = Convert.ToInt32(DrpPriority.SelectedValue);
                            Storage.Save(pro);
                        }
                        else
                        {
                            pro = new mdc_Priority();
                            pro.Process = TaskAPI.Process;
                            pro.Incident = TaskAPI.Incident;
                            pro.Business_Priority = Convert.ToInt32(DrpPriority.SelectedValue);
                            Storage.Save(pro);
                        }

                    }


                    if ((TaskAPI.Step == "DMO Maintain" || TaskAPI.Step == "DMO Approve" || TaskAPI.Step == "DMO Check") && !string.IsNullOrEmpty(this.DMO_DrpPriority.SelectedValue) && !string.IsNullOrEmpty(DrpPriority.SelectedValue))
                    {

                        //mdc_Priority pro;
                        //pro = (mdc_Priority.process == TaskAPI.Process & mdc_Priority.incident == TaskAPI.Incident).ListFirst<mdc_Priority>();
                        //pro.DMO_Priority = Convert.ToInt32(DMO_DrpPriority.SelectedValue);
                        //Storage.Save(pro);
                        mdc_Priority pro;
                        if ((mdc_Priority.process == TaskAPI.Process & mdc_Priority.incident == TaskAPI.Incident).Count<mdc_Priority>() > 0)
                        {
                            pro = (mdc_Priority.process == TaskAPI.Process & mdc_Priority.incident == TaskAPI.Incident).ListFirst<mdc_Priority>();
                            pro.DMO_Priority = Convert.ToInt32(DMO_DrpPriority.SelectedValue);
                            Storage.Save(pro);
                        }
                        else
                        {
                            pro = (mdc_Priority.process == TaskAPI.Process & mdc_Priority.incident == TaskAPI.Incident).ListFirst<mdc_Priority>();
                            pro.DMO_Priority = Convert.ToInt32(DMO_DrpPriority.SelectedValue);
                            Storage.Save(pro);
                        }


                    }
                    #endregion
                    #region “DMO Maintain”步骤
                    if (TaskAPI.Step == "DMO Maintain" || TaskAPI.Step == "DMO Approve" || TaskAPI.Step == "DMO Check")
                    {

                        TaskAPI["DMO_DrpPriority"] = DMO_DrpPriority.SelectedItem.Text;
                        (prm_promotion.process == TaskAPI.Process & prm_promotion.incident == TaskAPI.Incident).Edit<prm_promotion>(p =>
                        {
                            // p.SAP_Code = SAP_Code.Text;
                            p.DMO_DrpPriority = DMO_DrpPriority.SelectedValue;
                            p.DMO_Arrpvoe_Creation_Number = DMO_Arrpvoe_Creation_Number.Text;
                            p.DMO_Arrpvoe_Creation_Time = DMO_Arrpvoe_Creation_Time.Text;
                            p.DMO_Maintain_Creation_Number = DMO_Maintain_Creation_Number.Text;
                            p.DMO_Maintain_Creation_Time = DMO_Maintain_Creation_Time.Text;
                            p.DMO_Check_Creation_Number = DMO_Check_Creation_Number.Text;
                            p.DMO_Check_Creation_Time = DMO_Check_Creation_Time.Text;
                            p.DMO_Arrpvoe_Revision_Number = DMO_Arrpvoe_Revision_Number.Text;
                            p.DMO_Arrpvoe_Revision_Time = DMO_Arrpvoe_Revision_Time.Text;
                            p.DMO_Maintain_Revision_Number = DMO_Maintain_Revision_Number.Text;
                            p.DMO_Maintain_Revision_Time = DMO_Maintain_Revision_Time.Text;
                            p.DMO_Check_Revision_Number = DMO_Check_Revision_Number.Text;
                            p.DMO_Check_Revision_Time = DMO_Check_Revision_Time.Text;

                            //p.DMO_SalesSAP_Code = DMO_SalesSAP_Code.Text;
                        }
                        );
                    }
                    #endregion
                    Storage.Save(Assessment1.Assessment);    //审批意见

                    //多节点退回
                    if (!string.IsNullOrEmpty(Assessment1.JumpStep))
                    {
                        Assessment1.SetAsReturn(true);
                        TaskAPI.DirectActive(Assessment1.JumpStep);
                    }
                    else
                    {
                        Assessment1.SetAsReturn(false);
                    }
                    Assessment1.SubmitTo();

                    flg = TaskAPI.Send();
                });

                //如果数据库写入成功且流程发起成功的情况，将业务表数据与流程实例号进行关联
                if (TaskAPI.IsInitiate)
                {
                    if (!string.IsNullOrEmpty(DrpPriority.SelectedValue))
                    {
                        (mdc_Priority.process == TaskAPI.Process & mdc_Priority.incident == 0).Edit<mdc_Priority>(p =>
                        {
                            p.Incident = TaskAPI.Incident;
                        });
                    }
                    (prm_promotion.process == TaskAPI.Process & prm_promotion.incident == 0 & prm_promotion.userName == UserAPI.UserName).Edit<prm_promotion>(p =>
                    {
                        p.Incident = TaskAPI.Incident;
                    });

                    (t_Assessment.process == TaskAPI.Process & t_Assessment.incident == 0 & t_Assessment.userName == UserAPI.UserName).Edit<t_Assessment>(p =>
                    {
                        p.Incident = TaskAPI.Incident;
                    });

                    (t_Attachment.process == TaskAPI.Process & t_Attachment.incident == 0 & t_Attachment.username == UserAPI.UserName).Edit<t_Attachment>(p =>
                    {
                        p.Incident = TaskAPI.Incident;
                    });


                    //(prm_promotion1.process == TaskAPI.Process & prm_promotion1.incident == 0 & prm_promotion1.userName == UserAPI.UserName).Edit<prm_promotion1>(p =>
                    //{
                    //    p.Incident = TaskAPI.Incident;
                    //});


                }
                #endregion

                Send.Visible = false;
                Anthem.Manager.AddScriptForClientSideEval("JSInfomation('" + Resources.Global.C_00_Success + "',true)");
            }
            catch (Exception ex)
            {
                TaskDB.Log("", false, "调用NVA方法Send_Click出错：" + ex.ToString());
                Anthem.Manager.AddScriptForClientSideEval("JSInfomation('" + Resources.Global.C_00_Failed + "',false)");
            }
        }
        #endregion

        

        #region 事件 Category变换
        protected void VD_Category_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetEnable((VD_Category.SelectedValue == "1" || VD_Category.SelectedValue == "2" || VD_Category.SelectedValue == "3" || VD_Category.SelectedValue == "4"), VD_Category,DrpPriority);

        }
        #endregion

        //#region 事件 Tabs切换
        //protected void Tabs_Click(object sender, EventArgs e)
        //{
        //    if (((LinkButton)sender).CssClass == "tabs2")
        //    {
        //        return;
        //    }

        //    #region 初始化
        //    SetVisible(false, P_VD, P_SA);
        //    VD_L.CssClass = "tabs1";
        //   // SA_L.CssClass = "tabs1";
        //    //SA2_L.CssClass = "tabs1";
        //    #endregion

        //    #region Tabs切换动作
        //    switch (((LinkButton)sender).ID)
        //    {
        //        case "VD_L":
        //            VD_L.CssClass = "tabs2";
        //            P_VD.Visible = true;
        //            break;
        //        //case "SA_L":
        //        //    SA_L.CssClass = "tabs2";
        //        //    P_SA.Visible = true;
        //        //    break;
        //        //case "SA2_L":
        //        //    SA2_L.CssClass = "tabs2";
        //        //    P_SA2.Visible = true;
        //        //    break;
        //    }
        //    #endregion
        //}
        //#endregion

        //#region 方法 初始化Tabs
        //protected void InitTabs()
        //{
        //    VD_L.Visible = VD.Checked;
        //    //SA_L.Visible = SA.Checked;
        //    //SA2_L.Visible = SA2.Checked;

        //    SetVisible(false, P_VD);
        //    VD_L.CssClass = "tabs1";
        //    //SA_L.CssClass = "tabs1";
        //    //SA2_L.CssClass = "tabs1";

        //    //默认第1个显示 Tabs被选中
        //    if (VD_L.Visible)
        //    {
        //        VD_L.CssClass = "tabs2";
        //        P_VD.Visible = true;
        //        return;
        //    }

        //    //if (SA_L.Visible)
        //    //{
        //    //    SA_L.CssClass = "tabs2";
        //    //    P_SA.Visible = true;
        //    //    return;
        //    //}

        //    //if (SA2_L.Visible)
        //    //{
        //    //    SA2_L.CssClass = "tabs2";
        //    //    P_SA2.Visible = true;
        //    //    return;
        //    //}
        //}
        //#endregion

        #region 方法 InitDropdownList
        private void InitDropDownList()
        {
            if (!IsLoaded)
            {

                IsLoaded = true;
            }
        }
        #endregion

        //#region 方法 Save(保存数据库)
        //public void Save()
        //{
            

        //    foreach (prm_promotion1 lpm_1 in (List<prm_promotion1>)gv_List_Price.DataSource)
        //    {
        //        if (string.IsNullOrEmpty(lpm_1.Price_Grp) && string.IsNullOrEmpty(lpm_1.Subline) && string.IsNullOrEmpty(lpm_1.Sales_Office) && string.IsNullOrEmpty(lpm_1.LP_Status) && string.IsNullOrEmpty(lpm_1.Description) && string.IsNullOrEmpty(lpm_1.Proc_Stat) && string.IsNullOrEmpty(lpm_1.Scale_Quantity) && string.IsNullOrEmpty(lpm_1.Unit) && string.IsNullOrEmpty(lpm_1.Per) && string.IsNullOrEmpty(lpm_1.Calculat_Type) && string.IsNullOrEmpty(lpm_1.ScaleBasis))
        //        {
        //            continue;
        //        }
        //        lpm_1.Process = TaskAPI.Process;
        //        lpm_1.Incident = TaskAPI.Incident;
        //        lpm_1.UserName = UserAPI.UserName;
        //        lpm_1.Time = System.DateTime.Now;
        //        lpm_1.Status = 0;
        //        Storage.Save(lpm_1);
        //    }
          
        //}
        ////TaskAPI["Material"] = SEFI_Material;

        //#endregion

        //#region 事件 Add(增加行)
        //protected void Add_Click(object sender, EventArgs e)
        //{
        //    List<prm_promotion1> list_1_s = (List<prm_promotion1>)gv_List_Price.DataSource;
        //    prm_promotion1 list_1 = new prm_promotion1();
        //    list_1.No = list_1_s.Count + 1;
        //    //list_1_s.Insert(gv_List_Price.ChangedRowIndex == -1 || gv_List_Price.ChangedRowIndex >= list_1_s.Count ? list_1_s.Count + 1 : gv_List_Price.ChangedRowIndex+1, list_1);
        //    list_1_s.Insert(list_1_s.Count, list_1);
        //    gv_List_Price.DataSource = list_1_s;
        //    gv_List_Price.DataBind();
        //}
        //#endregion

        //#region 事件 Delete(删除行)
        //protected void Delete_Click(object sender, EventArgs e)
        //{
        //    //List<prm_promotion1> list_1_s = (List<prm_promotion1>)gv_List_Price.DataSource;
        //    //if (list_1_s.Count == 1)
        //    //{
        //    //    return;
        //    //}
        //    //list_1_s.RemoveAt(gv_List_Price.ChangedRowIndex == -1 || gv_List_Price.ChangedRowIndex >= list_1_s.Count ? list_1_s.Count - 1 : gv_List_Price.ChangedRowIndex);
        //    //gv_List_Price.DataSource = list_1_s;
        //    //gv_List_Price.DataBind();
        //    List<prm_promotion1> base_1_s = (List<prm_promotion1>)gv_List_Price.DataSource;
        //    IList<prm_promotion1> base1 = (prm_promotion1.process == TaskAPI.Process & prm_promotion1.incident == TaskAPI.Incident).List<prm_promotion1>();
        //    if (base_1_s.Count == 1)
        //    {

        //        return;
        //    }
        //    base_1_s.RemoveAt(gv_List_Price.ChangedRowIndex == -1 || gv_List_Price.ChangedRowIndex >= base_1_s.Count ? base_1_s.Count - 1 : gv_List_Price.ChangedRowIndex);
        //    bool flag = false;
        //    foreach (prm_promotion1 base_1 in base1)
        //    {

        //        foreach (prm_promotion1 base_2 in base_1_s)
        //        {
        //            if (base_2.ID == base_1.ID)
        //            {
        //                flag = true;
        //                break;
        //            }
        //        }
        //        if (!flag)
        //        {
        //            Storage.Delete(base_1);
        //        }
        //        flag = false;
        //    }
        //    gv_List_Price.DataSource = base_1_s;
        //    gv_List_Price.DataBind();
        //}
        //#endregion

        #region 方法 数据验证
        private List<string> DataValidation()
        {
            List<Regular> regs = new List<Regular>();
           
            //if (TaskAPI.Step == "DMO Approve" & Assessment1.Assessment.Opinion == "1")
            //{


            //    regs.Add(new Regular(RegularExp.NotEmpty, DMO_DrpPriority.SelectedValue, " Priority 必填"));


            //}

            if (TaskAPI.Step == "DMO Approve" & VD_Category.SelectedValue == "1" & Assessment1.Assessment.Opinion == "1")
            {

                if (DMO_Arrpvoe_Creation_Number.Text == "" | DMO_Arrpvoe_Creation_Time.Text == "")
                {
                    regs.Add(new Regular(RegularExp.NotEmpty, "", " DMO Approve的Creation项必填"));
                }

            }
            if (TaskAPI.Step == "DMO Maintain" & VD_Category.SelectedValue == "1" & Assessment1.Assessment.Opinion == "1")
            {

                if (DMO_Maintain_Creation_Number.Text == "" | DMO_Maintain_Creation_Time.Text == "")
                {
                    regs.Add(new Regular(RegularExp.NotEmpty, "", " DMO Maintain的Creation项必填"));
                }

            }
            if (TaskAPI.Step == "DMO Check" & VD_Category.SelectedValue == "1" & Assessment1.Assessment.Opinion == "1")
            {
                if (DMO_Check_Creation_Number.Text == "" | DMO_Check_Creation_Time.Text == "")
                {
                    regs.Add(new Regular(RegularExp.NotEmpty, "", " DMO Check的Creation项必填"));
                }

            }
            if (TaskAPI.Step == "DMO Approve" & VD_Category.SelectedValue != "1" & Assessment1.Assessment.Opinion == "1")
            {
                if (DMO_Arrpvoe_Revision_Number.Text == "" | DMO_Arrpvoe_Revision_Time.Text == "")
                {
                    regs.Add(new Regular(RegularExp.NotEmpty, "", " DMO Approve的Revision项必填"));
                }
               
            }
            if (TaskAPI.Step == "DMO Maintain" & VD_Category.SelectedValue != "1" & Assessment1.Assessment.Opinion == "1")
            {
                if (DMO_Maintain_Revision_Number.Text == "" | DMO_Maintain_Revision_Time.Text == "")
                {
                    regs.Add(new Regular(RegularExp.NotEmpty, "", " DMO Maintain的Revision项必填"));
                }

            }
            if (TaskAPI.Step == "DMO Check" & VD_Category.SelectedValue != "1" & Assessment1.Assessment.Opinion == "1")
            {
                if (DMO_Check_Revision_Number.Text == "" | DMO_Check_Revision_Time.Text == "")
                {
                    regs.Add(new Regular(RegularExp.NotEmpty, "", " DMO Check的Revision项必填"));
                }

            }


            return Validation(regs);
          
        }
        #endregion

        #region 方法 Others
        protected void VD_Payment_Methods_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        private string getFormCheckList(Anthem.CheckBoxList ck)
        {
            string t = "";
            for (int i = 0; i < ck.Items.Count; i++)
                if (ck.Items[i].Selected)
                    t += "," + ck.Items[i].Value;
                else
                    t += ",";
            return t;
        }
        private Anthem.CheckBoxList setCheckList(string t, Anthem.CheckBoxList ck)
        {
            try
            {
                string[] tt = t.Split(',');

                for (int i = 0; i <= tt.GetUpperBound(0); i++)
                {
                    for (int j = 0; j < ck.Items.Count; j++)
                    {
                        if (ck.Items[j].Value == tt[i].ToString())
                        { ck.Items[j].Selected = true; break; }
                    }
                }
            }
            catch { }
            return ck;
        }
        #endregion


    }
}